#include "ASLoadingScreen.h"
#include "..\app\ASAppConst.h"
#include "ASScreenMgr.h"
#include "ASCustomScreen.h"
#include "..\app\ASAppImgInfo.h"
#include "..\app\ASFTIo.h"
#include "ASScreenShare.h"
#include "..\screen\ASConfirmScreen.h"
#include "..\app\ASApp.h"

static as_loading_screen* s_as_ld_scr_p;

void as_loading_screen_add_logo(as_screen* scr_p)
{
    as_rect area;
    ASINT w, h;
    ASUINT param[8];
    as_get_internal_img_res_size(IMG_LOGO, &w, &h);
    area.w = (ASUINT16)w;
    area.h = (ASUINT16)h;
    area.x = (ASINT16)(as_get_lcd_width() - area.w) / 2;
    area.y = (ASINT16)(as_get_lcd_height() - area.h) / 2;
    param[0] = AS_IMAGE_FROM_RES;
    param[1] = (ASUINT)as_ucs2_from_ascii(IMG_LOGO);
    param[2] = ASFALSE;
    scr_p->add_ctrl(scr_p, AS_CTRL_IMAGE, area, 3, (void**)param);
}



static void as_loading_screen_timer_cb(ASINT param)
{
    as_destroy_timer(s_as_ld_scr_p->tid);

    if(as_if_has_simcard() == ASFALSE)
    {
        as_show_confirm_screen(TEXT_NO_SIMCARD, ASNULL, TEXT_OK, ASNULL, as_exit_app);
        return;
    }

    as_log_app("has simcard");
    /*prepare settings*/
    as_ft_load_setting();

    if(as_ft_check_if_show_advbar())
        as_app_set_show_advbar(ASTRUE);
    else
        as_app_set_show_advbar(ASFALSE);

    as_screen_go_back_nodraw();
    as_enter_custom_screen();
}

void as_loading_screen_init(as_screen* screen_p)
{
    as_loading_screen* ld_scr_p = (as_loading_screen*)screen_p;
    s_as_ld_scr_p = ld_scr_p;
    screen_p->set_bg_color(screen_p, LOADING_BG_COLOR);
    as_loading_screen_add_logo(screen_p);
    as_ft_gen_dir();
    as_ft_gen_default_data();
    ld_scr_p->tid = as_create_timer(1000, as_loading_screen_timer_cb);
}


void as_loading_screen_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_enter_loading_screen(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    as_screen* scr_p;
    as_ret ret;
    as_log_ui("<ui>----as_enter_loading_screen,scr_mgr is 0x%x", scr_mgr);
    ret = scr_mgr->new_and_push_screen(AS_SCREEN_LOADING);
    as_log_ui("<ui>----~~~~~~~~~ret[%d]", ret);
    scr_p = scr_mgr->get_cur_screen();
    as_log_ui("<ui>----~~~~~~~~~scr_p[%x]", scr_p);

    if(scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
    else
    {
        as_rect rect;
        rect.x = rect.y = 0;
        rect.w = as_get_lcd_width();
        rect.h = as_get_lcd_height();
        as_fill_rect(&rect, 0xff0000, 0x00ff00);
        as_flush();
    }
}